<template>
  <!-- 添加或修改菜单对话框 -->
  <ElementDialog :title="!form.id ? '新增' : '修改'" :visible.sync="visible">
    <ElForm ref="dataForm" :model="form" :rules="rules" label-width="80px">
      <ElFormItem label="整改措施主单" prop="name">
        <ElInput v-model="form.rectifyNum" placeholder="请输入整改措施主单" />
      </ElFormItem>
      <ElFormItem label="责任人名称" prop="code">
        <ElInput v-model="form.dutyUsername" placeholder="请输入责任人名称" />
      </ElFormItem>
      <ElFormItem label="责任部门名称" prop="code">
        <ElInput v-model="form.dutyName" placeholder="请输入责任部门名称" />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <div class="dialog-footer">
        <ElButton type="primary" @click="dataFormSubmit">确 定</ElButton>
        <ElButton @click="visible = false">取 消</ElButton>
      </div>
    </template>
  </ElementDialog>
</template>

<script>
  import { addObj, getObj, putObj } from '@/api/problem/rectifydetailed'

  export default {
    name: 'RectifyDetailedForm',
    data() {
      return {
        // 是否显示弹出层
        visible: false,
        form: {
          name: undefined,
          sort: 999,
          code: ''
        },
        // 表单校验
        rules: {
          rectifyNum: [
            {
              required: true,
              message: '菜单名称不能为空',
              trigger: 'blur'
            }
          ],
          dutyUsername: [
            {
              required: true,
              message: '菜单顺序不能为空',
              trigger: 'blur'
            }
          ],
          dutyName: [
            {
              required: true,
              message: '部门编号不能为空',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      init(isEdit, id) {
        if (id !== null) {
          this.form.parentId = id
        }
        this.visible = true
        this.getTreeselect()
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          if (isEdit) {
            getObj(id).then(response => {
              this.form = response.data.data
            })
          } else {
            this.form.deptId = undefined
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            if (this.form.parentId === undefined) {
              this.form.parentId = 0
            }

            if (this.form.id) {
              putObj(this.form).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            } else {
              addObj(this.form).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      },
      /** 转换菜单数据结构 */
      normalizer(node) {
        // if (node.children && !node.children.length) {
        //   delete node.children
        // }

        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      }
    }
  }
</script>
